package antlr4.myantlrtest.test.calc;

import antlr4.myantlrtest.gen.calc.CalcLexer;
import antlr4.myantlrtest.gen.calc.CalcParser;
import cn.hutool.core.io.FileUtil;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class MarvelCalc {
    public static void main(String[] args) throws Exception  {
        double v = 0.3D;
        float f = -0.3f;
        Long l = 5L;
        long aa = 5;



        // 从文件读取源代码
        String inputFile = null;
        if ( args.length>0 ) {
            inputFile = args[0];
        } else {
            System.out.println("语法格式：MarvelCalc inputfile");
            return;
        }
        InputStream is = System.in;
        File file = FileUtil.file(inputFile);

        if (file == null) {
            System.out.println("没找到文件" + inputFile);
            return;
        }
        is = new FileInputStream(file);

//        if ( inputFile!=null ) is = new FileInputStream(inputFile);


        CharStream input = CharStreams.fromStream(is);

        CalcLexer lexer = new CalcLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CalcParser parser = new CalcParser(tokens);
        ParseTree tree = parser.prog(); // 分析源代码

        EvalVisitor eval = new EvalVisitor();


        eval.visit(tree);
    }
}
